<?php

/*
*
* BEGIN engine.php
*
* 																											
* Dazzler Main Engine
* @Description: marshalls the calls for classes and autoloads a class name from either the base or the extensions folder
* @Authors: Salman Noor, Wits University
* @Authors: Thato Selebog, Wits University
* @Copyright: © Wits University 2011 - All Rights Reserved
* @License: BSD License (http://www.linfo.org/bsdlicense.html)
*
*
* BEGIN CHANGELOG
* Developers working on thee files are REQUIRED to maintain this CHANGELOG.
* Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
* !==================================================================================================================================================================
*
* VERSION 1.0.0
*	+ File Created, September, 5 September 2011
*	! Modified the way extensions and files are included, Salman, 6 September 2011
*
* VERSION 1.0.1
*	+ Added the fetched path items class in model to help find extensions, SAlman, 6 July 2011
*
* VERSION 1.0.2
*	+ Added the database adapter function to provide a singleton database connection that can be used by any class in the system to access the database, Thato, 19 September 2011
* ==================================================================================================================================================================!
* END CHANGELOG
*/


/*******************************************************************************************************************************************************************
*
* BEGIN Direct Call Check
*
/******************************************************************************************************************************************************************/
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1)
{
	/* Read the ini language file */
	$ini_lang_array = array();
	$clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	$lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
	die ($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}

/*******************************************************************************************************************************************************************
*
* END Direct Call Check
*
/******************************************************************************************************************************************************************/


class baseClass
{
        protected $lang_ini_array;
	function __construct()
	{
		$this->parseLanguageErrorLogFile();

	}
	
	function __destruct()
	{
		/* probably unnecessary, but here for extensibility anyway */
	}

        protected function parseLanguageErrorLogFile()
        {
                $this->lang_ini_array = array();
                $clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
                $this->lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
        }

        //Get a singleton instance of the adapter
        public function databaseAdapter()
        {
            //Check if connection to the database is enabled
            /* Read the config file */
            $ini_array = array();
            $ini_array = parse_ini_file("config.ini", true);
            $enabledatabase                                         = $ini_array['Database']['enabledatabaseconnection'];
            $databaseservername					= $ini_array['Database']['servername'];
            $databaseusername					= $ini_array['Database']['username'];
            $databasepassword					= $ini_array['Database']['password'];
            $databasename						= $ini_array['Database']['databasename'];
            if ($enabledatabase == "1")
            {
                //Connect to database
                $config = array("$databaseservername", "$databaseusername", "$databasepassword", "$databasename");
                $adapter = MySQLAdapter::getInstance($config);
                try{
                $adapter->connect();
                return $adapter;
                }

                catch(MySQLAdapterException $e)
                {
                    echo $e->errorMessage();
                }
            }
            else
            {
                /* Read the ini language file */
                $this->parseLanguageErrorLogFile();
                die ($this->lang_ini_array['ERRORS']['DBConnectionDisabled']);
            }

        }
}


function __autoload($class_name)
{
	global $path;
	global $modelpath;
	global $rootpath;
		
	/* First check base classes */
	if (file_exists($modelpath . "/" . $class_name . "/". $class_name .".php"))
	{
		include $modelpath . "/" . $class_name . "/". $class_name .".php";
	}
	else
	{
	/* second check UI classes */
	if (file_exists($modelpath . "/UIs/". $class_name .".php"))
	{
		include $modelpath . "/UIs/". $class_name .".php";
	}

//            $fetchpathitems = new fetchpathitems("$path/model/UIs", 1, "folder");
//		foreach ($fetchpathitems->cm as $pathitem)
//		{
//   	if (file_exists($modelpath . "/" . $class_name . "/". $class_name .".php"))
//	{
//                    include $pathitem . "/$class_name.php";
//        }
//			if (preg_match("/". $class_name ."/", $pathitem))
//			{
//				if (is_file($pathitem . "/$class_name.php"))
//				{
//
//				}
//				else
//				{
//					/* TODO: Put in download code to download extension from the central repo */
//				}
//			}
//		}

//                		$fetchpathitems = new fetchpathitems("$path/sites/". $_SERVER['HTTP_HOST'] . "/tests", 1, "folder");
//		foreach ($fetchpathitems->cm as $pathitem)
//		{
//			if (preg_match("/". $class_name ."/", $pathitem))
//			{
//				if (is_file($pathitem . "/$class_name.php"))
//				{
//					include $pathitem . "/$class_name.php";
//				}
//				else
//				{
//					/* TODO: Put in download code to download extension from the central repo */
//				}
//			}
//		}
	}
}

?>